package com.bootdo.common.controller;

import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import org.apache.poi.ss.usermodel.Workbook;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.domain.GenTableColumnDO;
import com.bootdo.common.service.GenTableColumnService;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 代码生成业务表字段
 *
 * @author rhgs
 * @email rhgs@163.com
 * @date 2024-01-22 14:45:00
 */

@Controller
@RequestMapping("/common/genTableColumn")
public class GenTableColumnController {
    @Autowired
    private GenTableColumnService genTableColumnService;

    @RequestMapping("/exportColumn")
    @ResponseBody
    @RequiresPermissions("common:genTableColumn:add")
    void exportColumn(String type, HttpServletResponse response, @RequestParam Map<String, Object> params) throws Exception {
        List<GenTableColumnDO> genTableColumnDOS = genTableColumnService.list(params);
        String title = "代码生成业务表字段" + DateUtils.format(new Date());
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, null),
            GenTableColumnDO.class, genTableColumnDOS);
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(title, "utf-8") + ".xls");
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @GetMapping()
    @RequiresPermissions("common:genTableColumn:genTableColumn")
    String GenTableColumn() {
        return "common/genTableColumn/genTableColumn";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("common:genTableColumn:genTableColumn")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<GenTableColumnDO> genTableColumnList = genTableColumnService.list(query);
        int total = genTableColumnService.count(query);
        PageUtils pageUtils = new PageUtils(genTableColumnList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("common:genTableColumn:add")
    String add() {
        return "common/genTableColumn/add";
    }

    @GetMapping("/edit/{columnId}")
    @RequiresPermissions("common:genTableColumn:edit")
    String edit(@PathVariable("columnId") Long columnId, Model model) {
            GenTableColumnDO genTableColumn = genTableColumnService.get(columnId);
        model.addAttribute("genTableColumn", genTableColumn);
        return "common/genTableColumn/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("common:genTableColumn:add")
    public R save( GenTableColumnDO genTableColumn) {
        IdWorker idWorker = new IdWorker();
//        genTableColumn.setUuid(idWorker.nextId() + "");
        if (genTableColumnService.save(genTableColumn) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("common:genTableColumn:edit")
    public R update( GenTableColumnDO genTableColumn) {
            genTableColumnService.update(genTableColumn);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("common:genTableColumn:remove")
    public R remove( Long columnId) {
        if (genTableColumnService.remove(columnId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("common:genTableColumn:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] columnIds) {
            genTableColumnService.batchRemove(columnIds);
        return R.ok();
    }

}
